<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *视频管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','group_id','title','image','desc','video','isnet','videourl','status','ishome','ishot','istoutiao','iswechat','paixu','createtime');
	$list=pdo_getslice('jy_cmsarticle_video',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['group_title']=pdo_getcolumn('jy_cmsarticle_videogroup',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['group_id']),'title');

		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'视频列表'),
			array('op'=>'edit','title'=>'添加视频'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'视频id','type'=>'string'),
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'group_title','title'=>'分类','type'=>'string'),
			array('name'=>'istoutiao','title'=>'是否已推送头条号','type'=>'string'),
		//	array('name'=>'iswechat','title'=>'是否已推送企鹅号','type'=>'string'),
		//	array('name'=>'ishome','title'=>'是否首页','type'=>'switch','set'=>'set'),
			array('name'=>'ishot','title'=>'是否热门','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'是否显示','type'=>'switch','set'=>'set'),
		),
		'actions'=>array(
			array('type'=>'post','op'=>'toutiao','title'=>'推送到头条号','message'=>'确定推送该视频到头条号吗！','show'=>''),
			),
		),
		'search'=>array(
			array('name'=>'title','title'=>'视频标题','type'=>'string','width'=>'15'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_cmsarticle_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回视频列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'名称','name'=>'title','help'=>'','ismust'=>true),
					array('type'=>'image','title'=>'图片','name'=>'image','help'=>'','ismust'=>true),
					array('type'=>'string','title'=>'描述','name'=>'desc','help'=>''),
					array('name'=>'group_id','title'=>'所属分类','type'=>'select','options'=>'group_id','option_title'=>'title','ismust'=>true),
					//array('type'=>'string','title'=>'视频路径','name'=>'video','help'=>'本地视频请填写'),
					array('type'=>'video','title'=>'视频','name'=>'video','help'=>'上传本地视频'),
				//	array('type'=>'switch','title'=>'是否引用网络视频','name'=>'isnet','help'=>''),
				//	array('type'=>'string','title'=>'视频地址','name'=>'videourl','help'=>'网络视频请填写','show'=>array('isnet'=>1)),
					array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
				//	array('type'=>'switch','title'=>'是否首页','name'=>'ishome','help'=>''),
					array('type'=>'switch','title'=>'是否热门','name'=>'ishot','help'=>''),
					array('type'=>'switch','title'=>'是否展示','name'=>'status','help'=>''),
				)
			),
		)
	);
	$return['group_id']=pdo_getall('jy_cmsarticle_videogroup',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['title'])){
		exi('请完善名称','error');
	}
	if(empty($postdata['image'])){
		exi('请完善图片','error');
	}
	if(!$postdata['group_id']){
		exi('请选择分类','error');
	}
	if(empty($postdata['video'])){
		exi('请上传视频','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'desc'=>trim($postdata['desc']),
		'video'=>trim($postdata['video']),
		'videourl'=>trim($postdata['videourl']),
		'likenum'=>intval($postdata['likenum']),
		'group_id'=>intval($postdata['group_id']),
		'readnum'=>intval($postdata['readnum']),
		'paixu'=>intval($postdata['paixu']),
		'isnet'=>intval($postdata['isnet']),
		'ishome'=>intval($postdata['ishome']),
		'ishot'=>intval($postdata['ishot']),
		'status'=>intval($postdata['status']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_cmsarticle_video',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_cmsarticle_video',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_cmsarticle_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的视频！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_cmsarticle_video',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishome'])){
		pdo_update('jy_cmsarticle_video',array('ishome'=>intval($post['ishome'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		pdo_update('jy_cmsarticle_video',array('ishot'=>intval($post['ishot'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='toutiao'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要推送的视频！','error');
	}
	$video=pdo_get('jy_cmsarticle_video',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));  //视频详情
	$data=cfc('jy_toutiao')->video(2,$video['video']);
	if($data['message']=='error'){
		exi('推送失败：'.$data['data'],'error');
	}
	exi('推送成功');
}
